﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

using HC = HandyControl.Controls;

namespace CommunityToolkitMvvmDemo.DependencyPropertys
{
    /// <summary>
    /// propa+两次tab
    /// </summary>
    public class PasswordBoxBindingHelper
    {
        public static string  GetPasswordContent(DependencyObject obj) => (string)obj.GetValue(PasswordContentProperty);
   
        public static void SetPasswordContent(DependencyObject obj, string value) => obj.SetValue(PasswordContentProperty, value);

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PasswordContentProperty =
            DependencyProperty.RegisterAttached("PasswordContent",
                typeof(string),
                typeof(PasswordBoxBindingHelper),
                new PropertyMetadata(string.Empty, OnPasswordContentPropertyChanged));

        /// <summary>
        /// PropertyChangedCallback将改变通知到PasswordBox.Password
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnPasswordContentPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //var box = d as HC.PasswordBox;

            var box = d as PasswordBox;
            box.PasswordChanged -= OnPasswordChanged;

            var password = (string)e.NewValue;
            if (box != null && box.Password != password)
                box.Password = password;

            //box.PasswordChanged += OnPasswordChanged;

        }

        /// <summary>
        /// PasswordChanged事件的响应来响应PasswordBox.Password的改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void OnPasswordChanged(object sender, RoutedEventArgs e)
        {
            //var box = sender as HC.PasswordBox;

            var box = sender as PasswordBox;
            SetPasswordContent(box, box.Password);
        }
    }
}
